precision mediump float;

uniform vec3 spotLight;
uniform vec4 LightDiffuseColor;
uniform vec4 LightSpecularColor;
uniform vec4 AmbientComponent;

uniform sampler2D u_texture0;
uniform sampler2D u_texture1;


varying vec2   v_uv;
varying vec3   v_Nomal;
varying vec3 v_tangentW;
varying vec3 v_bitangentW;
varying vec3   toEye;

void main()
{
	
	vec3 LightDirection=vec3(0.0,0.0,0.0)-spotLight;
	vec3 direction=normalize(LightDirection);
	vec3 normal = (texture2D(u_texture0,v_uv)).xyz;
    mat3 TBN = mat3(normalize(v_tangentW),normalize(v_bitangentW),normalize(v_Nomal));
    vec3 normalW = normalize((TBN * (2.0 * normal - 1.0)).xyz);
	
	//vec4 color = texture2D(u_texture1,v_uv);
	vec4 ObjColor = texture2D(u_texture1,v_uv);
	vec4 DiffuseComponent = max(dot(normalW, -direction), 0.0)* LightDiffuseColor;
	vec3 reflectVector = normalize((reflect(direction, normalW)));
	vec4 SpecularComponent=pow(max(dot(reflectVector,toEye),0.0),16.0)* LightSpecularColor;
	vec4 FinalColor = (AmbientComponent * 0.3 + DiffuseComponent * (1.0 - 0.3)) * ObjColor + SpecularComponent;
    //float diffuse = max(0.0, dot(-direction,normalW));
    //gl_FragColor = vec4(diffuse * color.xyz,color.a);
	gl_FragColor=FinalColor;
}
